# 参考中国大学mooc中‘Python玩转数据’一课中的文档
# 词云参考：https://github.com/amueller/word_cloud/blob/master/examples/masked.py
# 分词： https://github.com/fxsjy/jieba/

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import jieba
from wordcloud import WordCloud, STOPWORDS

# 测试文本
with open('./data_cloud.txt', 'r', encoding='UTF-8') as data:
	data_c = data.read()  # 读取数据
	
	# 进行分词
	seg_list = jieba.cut_for_search(data_c)
	seg_str = ','.join(seg_list)
	print(seg_list)
	alice_mask = np.array(Image.open('./huli.png')) # 打入背景图片
	stopwords = set(STOPWORDS) 

	wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask,stopwords=stopwords, contour_width=3, contour_color='steelblue',font_path = 'C:/Users/Windows/fonts/simkai.ttf')
	wc.generate(seg_str)

	# 显示
	plt.imshow(wc, interpolation='bilinear')
	plt.axis("off")
	plt.figure()
	plt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear')
	plt.axis("off")
	plt.show()
